package scream.servlet.cliente;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import scream.dao.ClienteDAO;
import scream.excepcion.DAOExcepcion;
import scream.modelo.Cliente;
import scream.negocio.SeguridadNegocioCliente;

/**
 * Servlet implementation class RegistrarCliente
 */
@WebServlet("/RegistrarCliente")
public class RegistrarCliente extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrarCliente() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String cod_cli = request.getParameter("txtCodigo");
		String nu_Ruc = request.getParameter("txtRuc");
		String nom_Razon = request.getParameter("txtRazonSocial");
		String Fecha = request.getParameter("txtFecha");
		String cant_emp = request.getParameter("txtCantidad");
		String empresa = request.getParameter("txtTipoEmpresa");
		
		//System.out.println(cod_cli);
		
		SeguridadNegocioCliente negocio = new SeguridadNegocioCliente();

		try {
			ClienteDAO cliDAO = new ClienteDAO();
			Collection<Cliente> cli = null;
			
			cli = cliDAO.buscarxRUCPoder(nu_Ruc);
			
			if(cli.size() != 0)
			{
			negocio.insertarCliente(cod_cli,nu_Ruc, nom_Razon, Fecha, cant_emp, empresa);
			request.setAttribute("Respuesta", "OK");
			RequestDispatcher rd = request.getRequestDispatcher("ExitoCliente.jsp");
			rd.forward(request, response);
			}
			else
			{
				request.setAttribute("Respuesta", "Fail");
				RequestDispatcher rd = request.getRequestDispatcher("ExitoCliente.jsp");
				rd.forward(request, response);
			}
		} catch (DAOExcepcion e) {
			request.setAttribute("Respuesta", "Exception");
			RequestDispatcher rd = request.getRequestDispatcher("ExitoCliente.jsp");
			rd.forward(request, response);
		}
		
		
		
	}

}
